import { ErrorMapper } from "utils/ErrorMapper";

import {libInterface}  from './lib/libInterface'
import MemHack from "utils/memory/memhack";
 
import { GameLoop } from "GameLoop/GameLoop";
import { Log } from "utils/log/TraceLog";
import mountWork from "GameLoop/WorldState/mount";
import { WorldState } from "GameLoop/WorldState/worldstate";
import { TaskManager } from "GameLoop/DecisionMaking/TaskManager";
import { WorkManager } from "GameLoop/Execution/WorkManager";
import { AdaptationLayer } from "GameLoop/AdaptationLayer/AdaptationLayer";
import {Analysis} from "./utils/tools/Analysis"
import profiler from "screeps-profiler";
import { DecisionIsCreepBoost } from "GameLoop/Execution/ControlManager/Control/Decision/creep/DecisionIsCreepBoost";
import { DecisionIsCreepTaskMode } from "GameLoop/Execution/ControlManager/Control/Decision/creep/DecisionIsCreepTaskMode";
import { CreepStateMachine } from "GameLoop/Execution/ControlManager/FSMFactory/FSMCreep/AICreepFactory";
import { RoomTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/RoomTaskAdapter";
import { CreepTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/CreepTaskAdapter";
import { TeamTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/TeamTaskAdapter";
import { ReactionTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/ReactionTaskAdapter";
import { BoostTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/BoostTaskAdapter";
import { CoreTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/CoreTaskAdapter";
import { FactoryTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/FactoryTaskAdapter";
import { PowerCreepTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/PowerCreepTaskAdapter";
import { TerminalTaskAdapter } from "GameLoop/AdaptationLayer/TaskAdapter/TerminalTaskAdapter";
import { ActionManager } from "GameLoop/Execution/ControlManager/Control/ActionManager/ActionManager";
import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { CacheManager } from "cache/CacheManager";
// import { StackAnalysis } from "utils/tools/StackAnalysis";
profiler.registerClass(WorldState,"WorldState")
profiler.registerClass(TaskManager,"TaskManager")
profiler.registerClass(AdaptationLayer,"AdaptationLayer")
profiler.registerClass(DecisionIsCreepBoost,"DecisionIsCreepBoost")
profiler.registerClass(DecisionIsCreepTaskMode,"DecisionIsCreepTaskMode")
profiler.registerClass(WorkManager,"WorkManager")
profiler.registerClass(ActionManager,"ActionManager")
profiler.registerClass(RoomTaskAdapter,"RoomTaskAdapter")
profiler.registerClass(CreepTaskAdapter,"CreepTaskAdapter")
profiler.registerClass(TeamTaskAdapter,"TeamTaskAdapter")
profiler.registerClass(ReactionTaskAdapter,"ReactionTaskAdapter")
profiler.registerClass(BoostTaskAdapter,"BoostTaskAdapter")
profiler.registerClass(PowerCreepTaskAdapter,"PowerCreepTaskAdapter")
profiler.registerClass(FactoryTaskAdapter,"FactoryTaskAdapter")
profiler.registerClass(TerminalTaskAdapter,"TerminalTaskAdapter")
profiler.registerClass(CoreTaskAdapter,"CoreTaskAdapter")
// import * as profile from "./utils/profiler";
//  const __PROFILER_ENABLED__ = false
// import { StackAnalysis } from "./utils/StackAnalysis";
// if(!Memory.ID_Incress)
// {
//     Memory.ID_Incress =0;
// }
/**妮露配色
 * 76A2B9
 * BFD9E5
 * D64F38
*/
/*
#2f2319
#aa4f23
#fed875

#6c627a
*/
mountWork();
// StackAnalysis.mount();
// (global as any).Profiler = profile.init();
global.wasmLib = new libInterface();
global.wasmLib.InitLibrary();
const generatePix=()=>
{
    if( Game.cpu.generatePixel && Game.cpu.bucket==10000)
    {
        Game.cpu.generatePixel();
    }
}
CacheManager.MemoryInit();
global.isinit= false;
global.GameLoop = new GameLoop();
(global as any).pathcache = StationNetwork.getInstance;
global.Log = new Log();
 
  profiler.enable();
 
//  (global as any).profiler = profiler
// Game.profiler.profile(10);
const gameloop =()=>
{
     // MemHack.pretick();
     if(! global.iswasmLoad)
     {
         return ;
     }
     if(global.isinit)
     {
         if(   Game.cpu.bucket>200 ||   Game.shard.name !="shard3")
         {
             global.GameLoop.run(); 
 
         }
         else    
         {
            global.GameLoop.run();
  
         }   
        
         generatePix();
     }
     else if(Game.shard.name.startsWith('shard'))
     {
         if(   Game.cpu.bucket>1000)
         {
             global.wasmLib.init_portal();
             global.GameLoop.init();
             global.isinit=true;
         }
     }
     else
     {
         if(   Game.cpu.bucket>300)
         {
             global.GameLoop.init();
             global.isinit=true;
         }
     }
}
export const loop =
//  StackAnalysis.wrap(
 
// Analysis.warpLoop(
// (
ErrorMapper.wrapLoop( () =>{
    // gameloop()
     profiler.wrap(gameloop)
     if(Memory.Debug['profile'])     
     {
        (Game as any).profiler.profile(Memory.Debug['profile'])
     }
})

//  )
// );
// let rec={};
// for(let k in Game.rooms)
// {
//     const room = Game.rooms[k];
//     if(room.getType() ==1  && room.controller.level>=6)
//     {
//         if(rec[room.getMinerial()[0].mineralType])
//         {
//             rec[room.getMinerial()[0].mineralType]++;
//         }
//         else 
//         {
//             rec[room.getMinerial()[0].mineralType]=1
//         }
//     }
// }
// console.log(JSON.stringify(rec));
// JSON.stringify(Game.rooms['W8N8'].findPath(new RoomPosition(31,22,'W8N8'),new RoomPosition(33,22,'W8N8')));


